﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Bot_Schedule.Lib;
using System.Net;
using Codeplex.Data;

namespace Bot_Schedule
{
    public partial class frm_themWeb : XtraForm
    {
        private string id = "";
        int state = 0;
        private dynamic olobj;
        private dynamic obj;
        private DataTable dtCustomer;
        public frm_themWeb(DataTable dtCustomer)
        {
            this.dtCustomer = dtCustomer;
            InitializeComponent();
            this.Text = this.Text + "( Tạo mới )";
            this.state = 0;
            cbKhach.Properties.DataSource = dtCustomer;
            cbKhach.Properties.DisplayMember = "Fullname";
            cbKhach.Properties.ValueMember = "Stt";
        }
        public frm_themWeb(dynamic obj, DataTable dtCustomer)
        {
            this.olobj = obj;
            this.dtCustomer = dtCustomer;
            InitializeComponent();
            this.Text = this.Text + "( Chỉnh sửa )";
            this.state = 1;
            textEdit1.EditValue = olobj.wsUrl;
            if (olobj.isEnable == "1") raActive.Checked = true;
            else raDeactive.Checked = true;
            cbKhach.Properties.DataSource = dtCustomer;
            cbKhach.Properties.DisplayMember = "Fullname";
            cbKhach.Properties.ValueMember = "Stt";
            cbKhach.EditValue = obj.customer_id;
        }
        void action(string key)
        {
            dynamic obj = null;
            if (key == "insertWeb")
            {
                obj = new
                {
                    wsUrl = textEdit1.EditValue.ToString(),
                    isEnable = raActive.Checked ? "1" : "0",
                    customer_id = cbKhach.EditValue.ToString()
                };
            }
            else
            {
                obj = new
                {
                    Stt = olobj.Stt,
                    wsUrl = textEdit1.EditValue.ToString(),
                    isEnable = raActive.Checked ? "1" : "0",
                    customer_id = cbKhach.EditValue.ToString()
                };
            }
            var json = DynamicJson.Serialize(obj);
            string resu = Functions.getdata(new WebClient(), key, json);
            if (resu == "1")
            {
                this.DialogResult = DialogResult.OK;
            }
            else MessageBox.Show("Sự cố khi thao tác với CSDL");
        }
        private void btAccept_Click(object sender, EventArgs e)
        {
            if (cbKhach.EditValue == null) { MessageBox.Show("Vui long chọn website"); return; }
            if (state == 0) action("insertWeb");
            else action("updateWeb");
            this.Close();
        }
    }
}
